#include "aky_com_include.h"


uint8 res = 0;
uint8 AKYAutoTestFlag = 0;
void auto_test_set_baud_rate(void);

void AutoTestMode(void) // 进入自动测试函数
{
    auto_test_set_baud_rate();
    g_s_cal = flash_get_cal_data();
    aky_sdadc_init();
    
    LCD_BL_ON;
	OLED_Clear();
    olde_fmt_num(0, 16,OLED_8X16,"cal_cnt=",g_s_cal.cal_cnt);
    OLED_Update();
    aky_tick_delay_ms(1000);

	OLED_Clear();
    
    wait_all_key_release();
    PUMP_OFF;
    VALVE_ON;
    zerotracing(0);
    wait_all_key_release();
    do
    {
        res = uart_get_cmd_stat();
        if (res == 1)
        {
            if (checke_cmd_ack(E_CMD_300_PRES_FOR_CALIBRATION) == 1)
            {
                g_s_cal.B1=stru_cmd_decode.receive_cal_pres*10; //接收到的校压值 保存到flash
                g_s_cal.cal_adc_val = (SensorCountw - ZeroCountw) / SpanValve;
                break;
            }
            else if (checke_cmd_ack(E_CMD_END_CMD) == 1)
            {
                send_ack_cmd(E_CMD_END_CMD);
                goto AUTO_TEST_OVER;
            }
            else if (checke_cmd_ack(E_CMD_START_COMMAND) == 1)
            {
                send_ack_cmd(E_CMD_START_COMMAND);
            }
        }
        else if (res == 0)
        {
        }
        calpressure(PRES_1000_ACCURACY);
        OLED_Clear();
    	OLED_ShowChinese(32, 0,E_NO_INVERSE, "实时压力值");
        sprintf(fmt_buf, "%03d:%d0", PressuremmHgw / 10,PressuremmHgw%10);
        OLED_ShowString(16,16,fmt_buf,OLED_16X32);
        OLED_ShowString(88,48,"mmHg",OLED_8X16);
        olde_fmt_num(0, 56,OLED_6X8,"",SensorCountw);

        OLED_Update();
        
        if (!KEY_POWER && !KEY_SET)
        {
            break;
        }
    } while (1);
    while (!KEY_POWER)
        ;
    while (!KEY_POWER)
        ;

    // RsensorCalibrate:
    g_s_cal.cal_adc_val = (SensorCountw - ZeroCountw) / SpanValve;

    if (g_s_cal.cal_adc_val <= AA1_EXPERIENCE_VALUE - AA1_EXPORIENCE_VALUE_ERROR_RANGE || g_s_cal.cal_adc_val >= AA1_EXPERIENCE_VALUE + AA1_EXPORIENCE_VALUE_ERROR_RANGE)
    {
        goto calerror;
    }
    else
    {
    }

    flash_save_cal_data(g_s_cal);
    g_s_cal = flash_get_cal_data();

    OLED_Clear();

    sprintf(fmt_buf, "%03d:%d", PressuremmHgw / 10,PressuremmHgw%10);
    OLED_ShowString(16,16,fmt_buf,OLED_16X32);
    OLED_ShowString(88,48,"mmHg",OLED_8X16);
    olde_fmt_num(0, 56,OLED_6X8,"",g_s_cal.cal_adc_val);

    OLED_Update();

    stru_cmd_decode.after_cal_pres = calpressure(PRES_100_ACCURACY);
    stru_cmd_decode.after_cal_pres_last = stru_cmd_decode.after_cal_pres;
    stru_cmd_decode.point_300_count = 0;
    stru_cmd_decode.point_zero_count = 0;

    send_ack_cmd(E_CMD_300_PRES_FOR_CALIBRATION);
    aky_tick_delay_ms(100);
    send_ack_cmd(E_CMD_300_PRES_FOR_CALIBRATION);
    aky_tick_delay_ms(100);
    send_ack_cmd(E_CMD_300_PRES_FOR_CALIBRATION);

    goto CalOverOut; //cal over
calerror:
	BB= VALVEOFF | PUMPOFF;
	iocontroloff();
    OLED_Clear();
    sprintf(fmt_buf, "ERRC");
    OLED_ShowString(50, 0,fmt_buf,OLED_8X16);
    OLED_Update();

    // SoundError();
    while (1)
    {
        res = uart_get_cmd_stat();
        if (res == 1)
        {
            if (checke_cmd_ack(E_CMD_300_PRES_FOR_CALIBRATION) == 1) //校压错误时 重发校压指令重新校压
            {
                stru_cmd_decode.after_cal_pres = 0;
                stru_cmd_decode.after_cal_pres_last = 0;
                send_ack_cmd(E_CMD_300_PRES_FOR_CALIBRATION);
                aky_tick_delay_ms(100);
                send_ack_cmd(E_CMD_300_PRES_FOR_CALIBRATION);
                aky_tick_delay_ms(100);
                send_ack_cmd(E_CMD_300_PRES_FOR_CALIBRATION);
            }
            else if (checke_cmd_ack(E_CMD_END_CMD) == 1)
            {
                send_ack_cmd(E_CMD_END_CMD);
                goto AUTO_TEST_OVER;
            }
        }
        else if (res == 0)
        {
        }

        if (!KEY_POWER && !KEY_SET)
        {
            break;
        }

    }

    // showerror();
    goto CalOverOut;
//*************************************************************************//新增改变功能设定
CalOverOut:
    while (!KEY_POWER)
        ;

    g_s_cal = flash_get_cal_data();

    OLED_Clear();
    OLED_ShowString(30, 0, "C-- MODE", OLED_8X16);
    OLED_Update();

    do
    {
    } while (!KEY_POWER);
    BB=POWER | VALVE;
    iocontrolon();
    do
    {
        res = uart_get_cmd_stat();
        if (res == 1)
        {
            if (checke_cmd_ack(E_CMD_300_PRES_FOR_COMPARING_LINEARITY) || checke_cmd_ack(E_CMD_200_PRES_FOR_COMPARING_LINEARITY) || checke_cmd_ack(E_CMD_100_PRES_FOR_COMPARING_LINEARITY))
            {
                calpressure(PRES_1000_ACCURACY);
                stru_cmd_decode.after_cal_pres = PressuremmHgw / 10;
                send_ack_cmd(stru_cmd_decode.last_decode_cmd);
            }
            else if (checke_cmd_ack(E_CMD_END_CMD) == 1)
            {
                send_ack_cmd(E_CMD_END_CMD);
                goto AUTO_TEST_OVER;
            }
            else  if (checke_cmd_ack(E_CMD_300_PRES_FOR_CALIBRATION) == 1)
            {
                stru_cmd_decode.after_cal_pres = stru_cmd_decode.after_cal_pres_last;
                send_ack_cmd(E_CMD_300_PRES_FOR_CALIBRATION);
                aky_tick_delay_ms(100);
                send_ack_cmd(E_CMD_300_PRES_FOR_CALIBRATION);
                aky_tick_delay_ms(100);
                send_ack_cmd(E_CMD_300_PRES_FOR_CALIBRATION);
            }
        }
        else if (res == 0)
        {
        }
        calpressure(PRES_1000_ACCURACY);

        OLED_Clear();
        OLED_ShowString(30, 0, "C-- MODE", OLED_8X16);
        sprintf(fmt_buf, "%03d:%d0", PressuremmHgw / 10,PressuremmHgw%10);
        OLED_ShowString(16,16,fmt_buf,OLED_16X32);
        OLED_ShowString(88,48,"mmHg",OLED_8X16);
        olde_fmt_num(0, 56,OLED_6X8,"",SensorCountw);
        OLED_Update();

        if (!KEY_POWER && !KEY_SET)
        {
            break;
        }
    } while (1);
    // DarkAllIcon();
AUTO_TEST_OVER:
    BB = VALVEOFF;
    iocontroloff();
    while (!KEY_POWER)
        ;
    IsPowerOn = 0;
}

// 检测自动工装通信
void TUDetect(void)
{
    off_time_tick_clr();
    while (1)
    {
        res = uart_get_cmd_stat();
        if (res == 1)
        {
            if (checke_cmd_ack(E_CMD_START_COMMAND) == 1)
            {
                send_ack_cmd(E_CMD_START_COMMAND);
                AutoTestMode(); // 进入自动测试函数
                break;
            }
        }
        else if (res == 0)
        {
        }

        if (get_cur_off_time_tick() > OFF_TIME_TICK_1S_COUNT * 3)
        {
            off_time_tick_clr();
            break;
        }
        if (get_any_key_pres() == 1)
        {
            break;
        }
    }
    auto_test_deinit();
}


void auto_test_set_baud_rate(void)
{
    it_simula_uart_init();
}

uint8 flag_is_auto_test_start = 0;
void auto_test_init(void)
{
    if (flag_is_auto_test_start == 0) //只初始化一次
    {
        flag_is_auto_test_start = 1;
        auto_test_set_baud_rate();
    }   
}

void auto_test_deinit(void)
{
   flag_is_auto_test_start = 0;
    it_simula_uart_deinit();
}

uint8 uart_rx_trig(void)
{
    uint8 res = 0;
    auto_test_init();
    if (uart_get_cmd_stat())
    {
        res = 1;
    }

    return res;
}


void auto_test_stop_mode(void)
{
    it_simula_uart_io_init();
    it_simula_uart_deinit();
#if (AKY_DEBUG_NO_UATOTEST_ENABLE == 0)
    P1W |= (1 << 7);

#endif


    flag_is_auto_test_start = 0;
    AKYAutoTestFlag = 0;
}
